import { addFav, removeFav, like, unlike, commentLike, commentUnlike } from "@/api/api"
export default {
    // 是否收藏
    favorites() {

        // 判断是否登录   否则跳去登录
        if (!this.checkLogin()) return

        // 判断是否收藏   是则取消收藏  否则收藏
        let { article: { is_fav }, uid, article_id } = this
        if (is_fav) {

            // 已收藏点击取消收藏
            removeFav({
                user_id: uid,
                article_id,
            }).then(res => {
                console.log(res)

                // 按钮变灰
                this.$toast(res.msg)
                if (res.code == 0) {
                    this.article.is_fav = false
                }
            })
        } else {

            // 未收藏点击收藏
            addFav({
                user_id: uid,
                article_id,
            }).then(res => {
                console.log(res)

                // 点亮按钮
                this.$toast(res.msg)
                if (res.code == 0) {
                    this.article.is_fav = true
                }
            })
        }
    },

    // 是否点赞
    iflike() {

        // 判断是否登录   否则跳去登录
        if (!this.checkLogin()) return

        // 判断是否点赞   是则取消点赞  否则点赞
        let { article: { is_like }, uid, article_id } = this
        if (is_like) {

            // 已点赞点击取消点赞
            unlike({
                user_id: uid,
                article_id,
            }).then(res => {
                console.log(res)

                // 按钮变灰
                this.$toast(res.msg)
                if (res.code == 0) {
                    this.article.is_like = false
                }
            })
        } else {

            // 未点赞点击点赞
            like({
                user_id: uid,
                article_id,
            }).then(res => {
                console.log(res)

                // 点亮按钮
                this.$toast(res.msg)
                if (res.code == 0) {
                    this.article.is_like = true
                }
            })
        }
    },


    // 发布评论点赞
    likeComment(comment_id, index, is_like) {
        // 判断是否登录   否则跳去登录

        // 判断是否发布评论点赞   是则取消发布评论点赞  否则发布评论点赞
        if (!this.checkLogin()) return;
        let {
            uid
        } = this;

        if (is_like) {
            // 已发布评论点赞点击取消发布评论点赞
            commentUnlike({
                user_id: uid,
                comment_id,
            }).then((res) => {

                console.log(res);
                // 按钮变灰  点赞数减少
                this.$toast(res.msg);
                if (res.code == 0) {

                    this.commentList[index].is_like = false;
                    this.commentList[index].like_count = res.count;
                }
            });
        } else {


            // 未发布评论点赞点击发布评论点赞

            commentLike({
                user_id: uid,
                comment_id,
            }).then((res) => {
                console.log(res);

                // 点亮按钮  点赞数增加
                this.$toast(res.msg);
                if (res.code == 0) {
                    //  让他高亮
                    this.commentList[index].is_like = true;
                    this.commentList[index].like_count = res.count;
                }
            });
        }
    },
}